/**
 * Created by dave on 17/7/3.
 */
var parse = parse;
var app = new Vue({
    el: '#myContract',
    data: {
        //跟路径
        ctx: '',
        list: true,
        contractInfo: false,
        openId: '',
        applicationId: '',
        endDate: '',//到期日期
        retainageDisable: true,
        start_date:$("#start_date_show").val(),
        end_date:$("#end_date_show").val()
    },
    created: function () {
        var that = this;
        that.ctx = $("#url_prefix").val();

        this.init();
    },
    mounted: function () {
    },
    methods: {
        init: function () {
            $("#retainage").val(0);
            $("#startDate").val("2019-01-02");
            this.$forceUpdate();
        },
        //隐藏加载项
        hidenTags: function () {
            $('#loadingToast').addClass('weui-actionsheet_toggle').fadeOut(200);
        },
        //显示加载项
        showTags: function () {
            $('#loadingToast').removeClass('weui-actionsheet_toggle').fadeIn(200);
        },
        //dataPicker
        datePickerStart: function () {
            var that = this;
            weui.datePicker({
                start:2017,
                end:2020,
                defaultValue: [2017,7,14],
                onChange: function(result){
                },
                onConfirm: function(result){
                    if(result[1].value < 10){
                        if(result[2].value < 10){
                            $("#startDate").val(dateFns.format(result[0].value + '-'+'0' + result[1].value + '-' + '0' + result[2].value,'YYYY-MM-DD'));
                        } else {
                            $("#startDate").val(dateFns.format(result[0].value + '-'+'0' + result[1].value + '-' + result[2].value,'YYYY-MM-DD'));
                        }
                    } else {
                        if(result[2].value < 10){
                            $("#startDate").val(dateFns.format(result[0].value + '-' + result[1].value + '-' + '0' + result[2].value,'YYYY-MM-DD'));
                        } else {
                            $("#startDate").val(dateFns.format(result[0].value + '-' + result[1].value + '-' + result[2].value,'YYYY-MM-DD'));
                        }
                    }
                    that.dateChange();
                },
                id: 'datePickerStart'
            });
        },
        datePickerEnd: function () {
            if($("#startDate").val() === ''){
                $.toptips("请输入起租日期");
                return;
            }
            var that = this;
            var startDate = dateFns.format(dateFns.subDays(dateFns.addMonths($("#startDate").val(), 2),1),'YYYY-MM-DD');
            var endDate = dateFns.format(dateFns.addMonths($("#startDate").val(), 13),'YYYY-MM-DD');
            weui.datePicker({
                start:startDate,
                end:endDate,
                onChange: function(result){
                },
                onConfirm: function(result){
                    if(result[1].value < 10){
                        if(result[2].value < 10){
                            $("#endDate").val(dateFns.format(result[0].value + '-'+'0' + result[1].value + '-' + '0' + result[2].value,'YYYY-MM-DD'));
                        } else {
                            $("#endDate").val(dateFns.format(result[0].value + '-'+'0' + result[1].value + '-' + result[2].value,'YYYY-MM-DD'));
                        }
                    } else {
                        if(result[2].value < 10){
                            $("#endDate").val(dateFns.format(result[0].value + '-' + result[1].value + '-' + '0' + result[2].value,'YYYY-MM-DD'));
                        } else {
                            $("#endDate").val(dateFns.format(result[0].value + '-' + result[1].value + '-' + result[2].value,'YYYY-MM-DD'));
                        }
                    }
                    that.dateChange();
                },
                id: 'datePickerEnd'
            });
        },




        dateChange: function () {
            var that = this;
            var start =dateFns.format($("#startDate").val(),'YYYY-MM-DD');
            var end = dateFns.format($("#endDate").val(),'YYYY-MM-DD');
            var monthMoney = $("#request_form").find("input[name=monthlyRent]").val();
            if(monthMoney !== '' && start !== 'NaN-NaN-NaN' && end !== 'NaN-NaN-NaN'){
                axios.get(that.ctx + '/wxtest/v2/applications/getLastDay?startDate='+start+'&endDate='+end+'').then(function(res){
                    $("#request_form").find("input[name=retainage]").val((monthMoney/30*res.data).toFixed(2));
                });
            }
        },
        //提交from，下一步
        submitBtn: function () {
            var that = this;
            var monthlyRent = $("#request_form").find("input[name=monthlyRent]").val();
            var monthlyRentValid = /^([1-9][0-9]*)+(.[0-9]{1,2})?$/.test(monthlyRent);
            if (monthlyRent == null || monthlyRent.trim() == "") {
                $.toptips("请输入月租金");
                return;
            }
            if(!monthlyRentValid){
                $.toptips("月租金为保留两位小数的正整数");
                return;
            }

            var startDate = $("#startDate").val();
            if (startDate == null || startDate.trim() == "") {
                $.toptips("请输入起租日期");

                return;
            }
            var endDate = $("#endDate").val();
            if (endDate == null || endDate.trim() == "") {
                $.toptips("请输入到期日期");
                return;
            }

            var retainage = $("#request_form").find("input[name=retainage]").val();
            if(Number(retainage) < 300) {
                $("#request_form").find("input[name=retainageMerge]").val(true)
            } else {
                $("#request_form").find("input[name=retainageMerge]").val(false)
            }
            var retainageValid = /^([1-9][0-9]*)+(.[0-9]{1,2})?$/.test(Number(retainage));
            if (retainage == null || retainage.trim() == "") {
                $.toptips("请输入尾款，无尾款输入0");
                return;
            }
            if(Number($("#retainage").val()) != 0){
                if(!retainageValid){
                    $.toptips("尾款不能小于0");
                    return;
                }
            }

            var phone = $("#request_form").find("input[name=mobile]").val();
            var phoneValid = /^(((13[0-9]{1})|(15[0-9]{1})|(14[0-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8})$/.test(phone);
            if (phone == null || phone.trim() == "") {
                $.toptips("请输入手机号");
                return;
            }
            if(!phoneValid){
                $.toptips("请输入正确格式的手机号");
                return;
            }

            if (!validateCodeFlag) {
                $.toptips("请先发送验证码");
                return;
            }

            var validateCode = $("#request_form").find("input[name=validateCode]").val();
            if (validateCode == null || validateCode.trim() == "") {
                $.toptips("请输入验证码");
                return;
            }

            startDate = startDate+" 00:00:00";
            var end = $("#endDate").val()+" 00:00:00";

            $("#request_form").find("input[name=startDate]").val(startDate);
            $("#request_form").find("input[name=endDate]").val(end);
            this.showTags();
            var url = that.ctx + "/wxtest/v2/applications/create";

            var param = $("#request_form").serialize();
            /*$.post(url,param,function(data){
                that.hidenTags();

                if (data.status ==200) {
                    var openId = $("#_openId").val();
                    window.location.href="/customer/application/info/"+data.data.id+"?openId="+openId;
                } else {
                    $.toptips(data.data.message);
                }

            },"json");*/


            $.ajax({
                url: url ,
                data:param,
                dataType: "json",
                type: "post",
                success: function (data) {
                    that.hidenTags();
                    if(data && data.status && data.status !==200){
                        $.toptips(data.message);
                    }else {
                        var openId = $("#_openId").val();
                        window.location.href=that.ctx + "/customer/application/info/"+data.data.id+"?openId="+openId;
                    }
                },
                error: function (xhr) {
                    that.hidenTags();
                    if(xhr.status === 403){
                        $.toptips(JSON.parse(xhr.response).message);
                    } else{
                        $.toptips("系统错误");
                    }
                }
            });
        },
        //查看
        checkDetail: function () {
            this.list = false;
            this.contractInfo = true
        }
    }
});
